package org.itboys.core.service.mysql.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.dao.wallet.SundayWalletMapper;
import org.itboys.core.dao.wallet.SundayWalletRecordMapper;
import org.itboys.core.entity.mongo.SundayMember;
import org.itboys.core.entity.mysql.wallet.SundayWallet;
import org.itboys.core.entity.mysql.wallet.SundayWalletExample;
import org.itboys.core.entity.mysql.wallet.record.SundayWalletRecord;
import org.itboys.core.entity.mysql.wallet.record.SundayWalletRecordExample;
import org.itboys.core.service.mongo.web.SundayMemberService;
import org.itboys.core.util.MybatisParamUtil;
import org.itboys.mysql.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author：贾琦磊
 * @Date：2018/12/3
 * @Description：web端钱包记录
 */
@Service
public class SundayWalletService extends BaseMySqlService<SundayWalletRecord> {
    @Autowired
    private SundayUploadService uploadService;
    @Autowired
    private SundayWalletRecordMapper recordMapper;
    @Autowired
    private SundayMemberService memberService;
    @Autowired
    private SundayWalletMapper walletMapper;

    public PageResult<SundayWallet> selectWallet(MybatisParamUtil paramUtil) {
        SundayWalletExample example = new SundayWalletExample();
        example.createCriteria()
                .andIsDeletedEqualTo(SundayCommonConstants.NO);
        long total = walletMapper.countByExample(example);
        example.setOrderByClause(paramUtil.getSortName());
        example.setSortOrder(paramUtil.getSortOrder());
        example.setPageSize(paramUtil.getRowSize());
        example.setStartRow(paramUtil.getRowStart());
        List<SundayWallet> data = walletMapper.selectByExample(example);
        if (ListUtil.isNotNull(data)) {
            List<Long> memberIds = new ArrayList<>();
            for (SundayWallet w : data
            ) {
                memberIds.add(w.getMemberId());
            }
            Map<String, Object> param = new HashMap<>();
            param.put("id in", memberIds);
            param.put("isDeleted", SundayCommonConstants.NO);
            List<SundayMember> members = memberService.list(param);
            for (SundayWallet w : data) {
                for (SundayMember m : members) {
                    if (w.getMemberId().equals(m.getId())) {
                        w.setMobile(m.getMobile());
                        w.setMemberName(m.getRealName());
                        break;
                    }
                }
            }
        }
        return new PageResult<>(data, total);
    }

    /**
     * 查询提现记录
     *
     * @param paramUtil
     * @param status
     * @return
     */
    public PageResult<SundayWalletRecord> selectPage(MybatisParamUtil paramUtil, Integer status, List<Integer> types, Long walletId) {
        SundayWalletRecordExample example = new SundayWalletRecordExample();
        SundayWalletRecordExample.Criteria criteria = example.createCriteria()
                .andIsDeletedEqualTo(SundayCommonConstants.NO);
        if (status != null) criteria.andStatusEqualTo(status);
        if (ListUtil.isNotNull(types)) criteria.andTypeIn(types);
        if (LongUtil.isNotZreo(walletId)) criteria.andWalletIdEqualTo(walletId);
        long total = recordMapper.countByExample(example);
        example.setOrderByClause(paramUtil.getSortName());
        example.setSortOrder(paramUtil.getSortOrder());
        example.setPageSize(paramUtil.getRowSize());
        example.setStartRow(paramUtil.getRowStart());
        List<SundayWalletRecord> data = recordMapper.selectByExample(example);
        if (ListUtil.isNotNull(data)) {
            List<Long> memberIds = new ArrayList<>();
            for (SundayWalletRecord r : data
            ) {
                memberIds.add(r.getMemberId());
            }
            Map<String, Object> param = new HashMap<>();
            param.put("isDeleted", SundayCommonConstants.NO);
            param.put("id in", memberIds);
            List<SundayMember> members = memberService.list(param);
            if (ListUtil.isNotNull(members)) {
                for (SundayWalletRecord r : data
                ) {
                    for (SundayMember m : members
                    ) {
                        if (r.getMemberId().equals(m.getId())) {
                            r.setMemberName(m.getRealName());
                            r.setMobile(m.getMobile());
                            break;
                        }
                    }
                }
            }
        }
        return new PageResult<>(data, total);
    }

    public void save(SundayWalletRecord record, MultipartFile file) {
        try {
            Map<String, String> backMap = uploadService.uploadSingle(file);
            if (StringUtils.isNotEmpty(backMap.get(SundayUploadService.UPLOAD_HALF_PATH))) {
                record.setVouch(backMap.get(SundayUploadService.UPLOAD_HALF_PATH));
            }
            if (LongUtil.isNotZreo(record.getId())) {
                recordMapper.update(record);
            } else {
                recordMapper.insert(record);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
